
<template>
  <div class="codeAssociated">
    <div>
      <pre class="previewhtml-main hljs">
              <code class="javascript">
      请先输入文件的绝对路径 路径地址大概为：E:\\static 然后在赋值代码 代码是双向绑定的 输入地址即可
      上传文件需要 formidable包 操作方法如下
      npm install formidable

      前端请求为from格式提交文件
              </code>
          </pre>
    </div>
    <div class="tonyon_list">
      保存文件绝对路径的地址：
      <el-input size="small" v-model="url" placeholder style="width:50%;margin-left: 11px;"></el-input>
    </div>
    <el-button @click="shengcheng">生成代码</el-button>
    <el-tabs v-model="activeName" v-if="is_shengcheng" class="tables0e" style="line-height: 23px;">
      <el-tab-pane label="controller/filesUploadbase64.js" name="1">
        <pre class="previewhtml-main hljs">
              <code class="javascript">
     let express = require('express');
     let router = express.Router();
     let filesUploadbase64 = require('../service/filesUploadbase64.js')
     filesUploadbase64 = new filesUploadbase64()

     /**
     * @api {post} /filesUploadbase64/add 图片上传
     * @apiDescription ""
     */
     router.post('/add', async function (req, res, next) {
         let data = await filesUploadbase64.add(req,res)
         res.send(data)
     });
     module.exports = router
              </code>
       </pre>
      </el-tab-pane>
      <el-tab-pane label="service/filesUploadbase64.js" name="2">
        <pre class="previewhtml-main hljs">
              <code class="javascript">
    class filesUploadbase64 {
        constructor() {
        }
        emptyData(msg) {
            Object.keys(msg).map(s => {
                if (msg[s] === '' || msg[s] === undefined) {
                    delete msg[s]
                }
            })
            return msg
        }
        verify(test, val) {
            for (var item in test) {
                if (!val[test[item].name]) {
                    return test[item].name + "为空"
                }
            }
        }
        async add(req, res) {
            try {
                var data = {}
                var fs = require("fs")
                var imgData = req.body.file
                var base64Date = imgData.replace(/^data:image\/\w+;base64,/, "")
                var dataBuffer = new Buffer(base64Data, 'base64')
                await new Promise(prevct => {
                    fs.writeFile("{{urlGo}}", dataBuffer, function(err) {
                        if(err){
                            res.send(err);
                        }else{
                            prevct(true)
                            res.send("保存成功！");
                        }
                    });
                })
                 
            } catch (error) {
                data = {
                    status: 500,
                    data: '未知错误'
                }
            }
            return data
        }
    }
    module.exports = filesUploadbase64 
              </code>
       </pre>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import hljs from "highlight.js";
export default {
  name: "App",
  data() {
    return {
      activeName: "1",
      is_shengcheng: "",
      urlGo:"",
      value: "",
      url: "",
      html: `<form action="/filesUploadbase64/add" method="post" enctype="multipart/form-data"></form>`,
      catalogue: "",
    };
  },
  async created() {},
  methods: {
    shengcheng() {
      this.is_shengcheng = false;
      this.urlGo = this.url.replace(/\\/g,'/')
      this.$nextTick(() => {
        this.is_shengcheng = true;
        this.$nextTick(() => {
          var pre = document.querySelectorAll(".previewhtml-main");
          Array.from(pre).map((v) => {
            hljs.highlightBlock(v);
          });
        });
      });
    },
  },
  mounted() {},
};
</script>
<style lang="scss" scoped="scoped">
.tonyon_list {
  width: 50%;
  display: inline-block;
  margin-top: 31px;
  margin-bottom: 31px;
}
</style>
